#include"Net.hpp"
#include"Dispatcher.hpp"


void onMessage(const zgwrpc::BaseConnection::ptr &conn, const zgwrpc::BaseMessage::ptr &msg)
{

    LOG(LogLevel::INFO)<<"调用回调";
    std::string body=msg->serialize();
    std::cout<<body<<std::endl;
    auto rpc_req=zgwrpc::MessageFactory::create<zgwrpc::RpcResponse>();
    rpc_req->setRCode(zgwrpc::RCode::RCODE_OK);
    rpc_req->setResult("我要完元神");
    rpc_req->setMType(zgwrpc::MType::RSP_RPC);
    cout<<"发送消息"<<endl;
    conn->send(rpc_req);
    
}

void OnMessageTopical(const zgwrpc::BaseConnection::ptr &conn, const zgwrpc::BaseMessage::ptr &msg)
{
    LOG(LogLevel::INFO)<<"接收到Topic请求";
    std::string body=msg->serialize();
    std::cout<<body<<std::endl;
    
    auto topic_rsp=zgwrpc::MessageFactory::create<zgwrpc::TopicResponse>();
    topic_rsp->setRCode(zgwrpc::RCode::RCODE_OK);
    topic_rsp->setMType(zgwrpc::MType::RSP_TOPIC);
    conn->send(topic_rsp);
}


int main()
{
    zgwrpc::Dispatcher::ptr dispatcher=zgwrpc::DispatcherFactory::create();
    dispatcher->registerHandler<zgwrpc::RpcRequest>(zgwrpc::MType::REQ_RPC,onMessage);
    dispatcher->registerHandler<zgwrpc::TopicRequest>(zgwrpc::MType::REQ_TOPIC,OnMessageTopical);



    auto server=zgwrpc::ServerFactory::create(9090);
    server->setMessageCallback(bind(&zgwrpc::Dispatcher::dispatch,dispatcher.get(),placeholders::_1,placeholders::_2));
    
    server->start();
}